﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineMovieTicket.Data.IRepository;
using OnlineMovieTicket.Model;

namespace OnlineMovieTicket.Data.Repository
{
    public class MoviesRepository : IMoviesRepository
    {
        ONLINEMOVIETICKETEntities dataContent;

        public MoviesRepository()
        {
            dataContent = new ONLINEMOVIETICKETEntities();
        }

        public IList<Movies> GetMovies()
        {
            var MovieList = (from dt in dataContent.Movies
                             select new Movies
                             {
                                 MovieCode = dt.MovieCode,
                                 MovieName = dt.MovieName,
                                 Language = dt.Language,
                                 Duration = dt.Duration,
                                 ReleaseDate = dt.ReleaseDate,
                                 Genre = dt.Genre,
                                 Subtitle = dt.Subtitle,
                                 Synopsis = dt.Synopsis,
                                 Trailer = dt.Trailer,
                                 Website = dt.Website,
                                 MovieCast = dt.MovieCast,
                                 MovieDirector = dt.MovieDirector,
                                 Distributor = dt.Distributor,
                                 Status = dt.Status,
                                 Image = dt.Image
                             }).ToList();
            return MovieList;
        }

        public IList<Movies> GetMovies(string movieStatus)
        {
            var MovieList = (from dt in dataContent.Movies
                             where dt.Status == movieStatus
                             select new Movies
                             {
                                 MovieCode = dt.MovieCode,
                                 MovieName = dt.MovieName,
                                 Language = dt.Language,
                                 Duration = dt.Duration,
                                 ReleaseDate = dt.ReleaseDate,
                                 Genre = dt.Genre,
                                 Subtitle = dt.Subtitle,
                                 Synopsis = dt.Synopsis,
                                 Trailer = dt.Trailer,
                                 Website = dt.Website,
                                 MovieCast = dt.MovieCast,
                                 MovieDirector = dt.MovieDirector,
                                 Distributor = dt.Distributor,
                                 Status = dt.Status,
                                 Image = dt.Image
                             }).ToList();
            return MovieList;
        }

        public IList<Movies> GetMoviesByRating()
        {
            var MovieList = (from dt in dataContent.Movies.Top("10")
                             select new Movies
                             {
                                 MovieCode = dt.MovieCode,
                                 MovieName = dt.MovieName,
                                 Language = dt.Language,
                                 Duration = dt.Duration,
                                 ReleaseDate = dt.ReleaseDate,
                                 Genre = dt.Genre,
                                 Subtitle = dt.Subtitle,
                                 Synopsis = dt.Synopsis,
                                 Trailer = dt.Trailer,
                                 Website = dt.Website,
                                 MovieCast = dt.MovieCast,
                                 MovieDirector = dt.MovieDirector,
                                 Distributor = dt.Distributor,
                                 Status = dt.Status,
                                 Image = dt.Image
                             }).ToList();
            return MovieList;
        }
        public Movies GetMovie(int MovieCode)
        {
            var rmovie = from dt in dataContent.Movies
                         where dt.MovieCode == MovieCode
                         select new Movies
                         {
                             MovieCode = dt.MovieCode,
                             MovieName = dt.MovieName,
                             Language = dt.Language,
                             Duration = dt.Duration,
                             ReleaseDate = dt.ReleaseDate,
                             Genre = dt.Genre,
                             Subtitle = dt.Subtitle,
                             Synopsis = dt.Synopsis,
                             Trailer = dt.Trailer,
                             Website = dt.Website,
                             MovieCast = dt.MovieCast,
                             MovieDirector = dt.MovieDirector,
                             Distributor = dt.Distributor,
                             Status = dt.Status,
                             Image = dt.Image
                         };

            return rmovie.First();
        }

        public void Save(Movies movie, int mode)
        {
            if (mode == 0)
            {
                dataContent.AddToMovies(new Movie
                {
                    MovieCode = movie.MovieCode,
                    MovieName = movie.MovieName,
                    Language = movie.Language,
                    Duration = movie.Duration,
                    ReleaseDate = movie.ReleaseDate,
                    Genre = movie.Genre,
                    Subtitle = movie.Subtitle,
                    Synopsis = movie.Synopsis,
                    Trailer = movie.Trailer,
                    Website = movie.Website,
                    MovieCast = movie.MovieCast,
                    MovieDirector = movie.MovieDirector,
                    Distributor = movie.Distributor,
                    Status = movie.Status,
                    Image = movie.Image
                });
            }
            else
            {
                var dbmovie = dataContent.Movies.FirstOrDefault(m => m.MovieCode == movie.MovieCode);

                dbmovie.MovieName = movie.MovieName;
                dbmovie.Language = movie.Language;
                dbmovie.Duration = movie.Duration;
                dbmovie.ReleaseDate = movie.ReleaseDate;
                dbmovie.Genre = movie.Genre;
                dbmovie.Subtitle = movie.Subtitle;
                dbmovie.Synopsis = movie.Synopsis;
                dbmovie.Trailer = movie.Trailer;
                dbmovie.Website = movie.Website;
                dbmovie.MovieCast = movie.MovieCast;
                dbmovie.MovieDirector = movie.MovieDirector;
                dbmovie.Distributor = movie.Distributor;
                dbmovie.Status = movie.Status;
                dbmovie.Image = movie.Image;
            }
            dataContent.SaveChanges();
        }

        public bool Delete(int movieCode)
        {
            bool returnVal = false;
            try
            {
                var data = dataContent.Movies.FirstOrDefault(m => m.MovieCode == movieCode);
                if (data != null)
                {
                    dataContent.Movies.DeleteObject(data);
                    dataContent.SaveChanges();
                    returnVal = true;
                }
                else
                    throw new Exception("Record not found");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnVal;
        }

        public int GetNewMovieCode()
        {
            int newCode = 0;

            IList<Model.Movies> movielist = GetMovies();

            if (movielist != null && movielist.Count > 0)
                newCode = movielist[movielist.Count - 1].MovieCode;

            return newCode + 1;
        }
    }
}
